package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.atguigu.service.UserInfoService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("userFollow")
public class UserFollowController {
    @Reference
    private UserFollowService userFollowService;

    @GetMapping("auth/follow/{id}")
    public Result follow(@PathVariable Long id, HttpServletRequest request){
        UserFollow userFollow = new UserFollow();
        userFollow.setHouseId(id);
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        System.out.println(userInfo);
        userFollow.setUserId(userInfo.getId());
        userFollowService.insert(userFollow);
        return Result.ok();
    }

    @GetMapping("auth/list/{pageNum}/{pageSize}")
    public Result list(HttpServletRequest request,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("USER");
        PageInfo<UserFollowVo> page = userFollowService.findListPage(pageNum,pageSize,userInfo.getId());
        return Result.ok(page);
    }
    @GetMapping("auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable Long id){
        userFollowService.cancelFollow(id);
        return Result.ok();
    }

}
